package com.example.day5_fragment2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.widget.RadioGroup;

import com.example.day5_fragment2.fragment.OneFragment;
import com.example.day5_fragment2.fragment.ThreeFragment;
import com.example.day5_fragment2.fragment.TwoFragment;

/***
 1。fragment回退栈：先进后出
 2。fragment嵌套
 3。fragment通信

 */
public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private OneFragment oneFragment;
    public TwoFragment twoFragment;
    private ThreeFragment threeFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup = findViewById(R.id.group);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                hideAll();
                switch (checkedId){
                    case R.id.rb1: {
                        FragmentManager manager = getSupportFragmentManager();
                        FragmentTransaction transaction = manager.beginTransaction();
                        if (oneFragment != null) {
                            transaction.show(oneFragment);
                        } else {
                            oneFragment = new OneFragment();
                            transaction.add(R.id.frame, oneFragment);
                        }
                        transaction.addToBackStack("one");//添加回退栈
                        transaction.commit();
                    }
                        break;
                    case R.id.rb2:
                    {
                        FragmentManager manager = getSupportFragmentManager();
                        FragmentTransaction transaction = manager.beginTransaction();
                        if (twoFragment != null) {
                            transaction.show(twoFragment);
                        } else {
                            twoFragment = new TwoFragment();
                            transaction.add(R.id.frame, twoFragment);
                        }

                        transaction.commit();
                    }
                        break;
                    case R.id.rb3:
                    {
                        FragmentManager manager = getSupportFragmentManager();
                        FragmentTransaction transaction = manager.beginTransaction();
                        if (threeFragment != null) {
                            transaction.show(threeFragment);
                        } else {
                            threeFragment = new ThreeFragment();
                            transaction.add(R.id.frame, threeFragment);
                        }

                        transaction.commit();
                    }
                        break;
                }
            }
        });
    }
    public void hideAll(){
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        if(oneFragment != null){
            transaction.hide(oneFragment);
        }
        if(twoFragment != null){
            transaction.hide(twoFragment);
        }
        if(threeFragment != null){
            transaction.hide(threeFragment);
        }
        transaction.commit();
    }
}
